﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.BizTalk.ExplorerOM;
using Microsoft.BizTalk.Operations;
using Microsoft.BizTalk.ObjectModel;
using System.Configuration;
using System.Diagnostics;

namespace BizTalkOneService
{
    class BizTalkPorts
    {
        EventLog appLog = new EventLog();
        private int ReceiveLocationsEnabled = 0;
        private int ReceiveLocationsDisabled = 0;
        private int ReceiveLocationsTotal = 0;
        private int SendPortsStarted = 0;
        private int SendPortsStopped = 0;
        private int SendPortsUnBound = 0;
        private int SendPortsTotal = 0;
        List<Counter> ReceiveLocationsList = new List<Counter>();
        List<Counter> SendPortsList = new List<Counter>();

        public BizTalkPorts()
        {
            try
            {
            BtsCatalogExplorer bts = new BtsCatalogExplorer();
            bts.ConnectionString = Convert.ToString(ConfigurationManager.ConnectionStrings["BizTalkConnectionString"]);
            ReceivePortCollection ReceivePorts = bts.ReceivePorts;
            
                foreach (ReceivePort Port in ReceivePorts)
                {
                    foreach (ReceiveLocation Location in Port.ReceiveLocations)
                    {
                        if (Location.Enable == true)
                            this.ReceiveLocationsEnabled++;
                        if (Location.Enable == false)
                            this.ReceiveLocationsDisabled++;
                        this.ReceiveLocationsTotal++;

                    }

                }
                string enabled = String.Format("Enabled ({0})",this.ReceiveLocationsTotal);
                string disabled = String.Format("Disabled ({0})", this.ReceiveLocationsTotal);
                string totalrcv = String.Format("Total ({0})", this.ReceiveLocationsTotal);
                ReceiveLocationsList.Add(new Counter { CounterCategory = "ReceiveLocation" , CounterName = enabled, CounterValue = this.ReceiveLocationsEnabled });
                ReceiveLocationsList.Add(new Counter { CounterCategory = "ReceiveLocation" , CounterName = disabled, CounterValue = this.ReceiveLocationsDisabled });
                ReceiveLocationsList.Add(new Counter { CounterCategory = "ReceiveLocation" , CounterName = totalrcv, CounterValue = this.ReceiveLocationsTotal });

                SendPortCollection SendPorts = bts.SendPorts;
                foreach (SendPort Port in SendPorts)
                {
                    if (Port.Status == PortStatus.Started)
                        this.SendPortsStarted++;
                    if (Port.Status == PortStatus.Stopped)
                        this.SendPortsStopped++;
                    if (Port.Status == PortStatus.Bound)
                        this.SendPortsUnBound++;
                    this.SendPortsTotal++;
                }
                string started = String.Format("Started ({0})", this.SendPortsTotal);
                string stopped = String.Format("Stopped ({0})", this.SendPortsTotal);
                string unbound = String.Format("Unenlisted ({0})", this.SendPortsTotal);
                string totalsnd = String.Format("Total ({0})", this.SendPortsTotal);
                SendPortsList.Add(new Counter { CounterCategory = "SendPort" , CounterName = started, CounterValue = this.SendPortsStarted });
                SendPortsList.Add(new Counter { CounterCategory = "SendPort" , CounterName = stopped, CounterValue = this.SendPortsStopped });
                SendPortsList.Add(new Counter { CounterCategory = "SendPort" , CounterName = unbound, CounterValue = this.SendPortsUnBound });
                SendPortsList.Add(new Counter { CounterCategory = "SendPort" , CounterName = totalsnd, CounterValue = this.SendPortsTotal });
            }
            catch (Exception ex)
            {
                appLog.Source = Settings.EventLogSource;
                EventLogEntryType eventType = EventLogEntryType.Error;
                appLog.WriteEntry("Error retrieving port status: " + ex.Message.ToString() + "\n" + ex.StackTrace , eventType); 
            }

        }
         
        public List<Counter> GetReceiveLocations()
        {
            return ReceiveLocationsList;
        }

        
        public List<Counter> GetSendPorts()
        {
            return SendPortsList;
        }
        
    }
}
